package com.iclient.request;

import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;


public class EnhancedHttpRequest extends HttpServletRequestWrapper{

	private final Map<String, String[]> modifiableParameters;
	private Map<String, String[]> allParameters = null;
	
	public EnhancedHttpRequest(final HttpServletRequest request, 
			final Map<String, String[]> additionalParams) {
		super(request);
		modifiableParameters = new HashMap<String, String[]>();
		modifiableParameters.putAll(additionalParams);
	}

	@Override
	public String getParameter(final String name) {
		String[] paramValue = getParameterMap().get(name);
		if(null != paramValue){
			return paramValue[0];
		}
		return super.getParameter(name);
	}

	@Override
	public Map<String, String[]> getParameterMap() {
		if(allParameters == null){
			allParameters = new TreeMap<>();
			allParameters.putAll(modifiableParameters);
		}
		return Collections.unmodifiableMap(allParameters);
	}
	
	
	@Override
    public Enumeration<String> getParameterNames(){
        return Collections.enumeration(getParameterMap().keySet());
    }

    @Override
    public String[] getParameterValues(final String name){
        return getParameterMap().get(name);
    }

}
